package com.pengcheng.taskapplication.mvp.view.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.pengcheng.taskapplication.R;
import com.pengcheng.taskapplication.adapter.ParkAdapter;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.model.ParkOrderModel;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenterV2;
import com.pengcheng.taskapplication.mvp.view.activity.ConfrimOrderActivity;
import com.pengcheng.taskapplication.net.IView;
import com.pengcheng.taskapplication.util.SPUtils;
import com.pengcheng.taskapplication.util.ToastUtils;

import java.lang.reflect.Type;
import java.util.ArrayList;

import butterknife.BindView;

public class MyParkListFragment  extends BaseFragment implements IView,ParkAdapter.OnStopLisener {
    private UserPresenterV2 userPresenter;
    @BindView(R.id.pull_to_refresh_lv)
    PullToRefreshListView pullToRefreshListView;
    ArrayList<ParkOrderModel> list =new ArrayList<>();
    private ParkAdapter adapter;


    @BindView(R.id.empty_view)
    View empty_view;

    private int page=0;
    private int pagesize=10;
    private String uid;
    private String newtoken;

    @Override
    protected int layoutResID() {
        return R.layout.fragment_park_list_;
    }

    @Override
    protected void initData() {
        uid= SPUtils.getString(context, ConstantValue.SpType.userid);
        newtoken= SPUtils.getString(context,ConstantValue.SpType.newtoken);

        userPresenter = new UserPresenterV2(this);

        //adapter
        adapter = new ParkAdapter(context,list);
        adapter.setOnStopLisner(this);
        //manager
        pullToRefreshListView.setAdapter(adapter);

        pullToRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ParkOrderModel model=(ParkOrderModel) adapterView.getAdapter().getItem(i);
                Intent intent =new Intent(context, ConfrimOrderActivity.class);
                intent.putExtra("from",1);
                intent.putExtra("tradno",model.getTradno());
                startActivity(intent);

            }
        });
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page=0;
                list.clear();
                queryParkOrderList(newtoken,uid,page,pagesize);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                queryParkOrderList(newtoken,uid,page,pagesize);
            }
        });
        pullToRefreshListView.setEmptyView(empty_view);
        queryParkOrderList(newtoken,uid,page,pagesize);
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        if(actionType.equals(ConstantValue.RequestKey.queryParkOrderList)){
            Gson gson= new Gson();
            Type type =new TypeToken<ArrayList<ParkOrderModel>>(){}.getType();
            ArrayList<ParkOrderModel> orderModels= gson.fromJson(gson.toJson(data),type);
            list.addAll(orderModels);
            adapter.notifyDataSetChanged();
            pullToRefreshListView.onRefreshComplete();
        }
    }

    @Override
    public void onFailed(String code, String msg) {
        pullToRefreshListView.onRefreshComplete();
    }
    public void queryParkOrderList(String token,String uid,int page,int pagesize){
        userPresenter.queryParkOrderList(token,uid,page,pagesize,ConstantValue.RequestKey.queryParkOrderList);
    }
    @Override
    public void onStop(ParkOrderModel parkOrderModel) {
        //停止机器
    }

    @Override
    public void onResume() {
        super.onResume();
        uid = SPUtils.getString(context, ConstantValue.SpType.userid);
        newtoken = SPUtils.getString(context, ConstantValue.SpType.newtoken);
    }
}
